<?php

namespace Backoffice\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Backoffice\Model\Entity\Page;

class PageController extends AbstractActionController
{
    /**
    * Entity manager instance
    *
    * @var Doctrine\ORM\EntityManager
    */
    protected $em;

    /**
    * Returns an instance of the Doctrine entity manager loaded from the service
    * locator
    *
    * @return Doctrine\ORM\EntityManager
    */
    
    public function getEntityManager()
    {
        if (null === $this->em) {
            $this->em = $this->getServiceLocator()
                ->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }
    
    public function indexAction()
    {
        return new ViewModel(
            array(
                'pages' => $this->getEntityManager()->getRepository('Backoffice\Model\Entity\Page')->findAll()
            )
        );
    }
    
    public function addAction()
    {
        $form = new \Backoffice\Form\PageForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $page = new Page();
            $form->setInputFilter($page->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $page->populate($form->getData());
                $this->getEntityManager()->persist($page);
                $this->getEntityManager()->flush();

                // Redirect to list of pages
                return $this->redirect()->toRoute('page');
            }
        }
        return array('form' => $form);
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('page', array(
                'action' => 'add'
            ));
        }
        $page = $this->getEntityManager()->find('Backoffice\Model\Entity\Page', $id);
        if (is_null($page)) {
            return $this->redirect()->toRoute('page');
        }

        $form  = new \Backoffice\Form\PageForm();
        $form->bind($page);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setInputFilter($page->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $form->bindValues();
                $this->getEntityManager()->flush();

                // Redirect to list of pages
                return $this->redirect()->toRoute('page');
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function deleteAction()
    {
        $error = null;
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('page');
        }

        $em = $this->getEntityManager();
        $page = $em->find('Backoffice\Model\Entity\Page', $id);
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                if ($page) {
                    // Supprimer la page
                    $em->remove($page);
                    $em->flush();
                }
            }

            // Redirect to list of pages
            return $this->redirect()->toRoute('page');
        }
        
        if (is_null($page)) {
            $error = "La page numero $id n'existe pas !";
        }

        return array(
            'id'    => $id,
            'page' => $page,
            'error' => $error,
        );
    }


}

